<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>async习题：考察js事件循环机制</title>

  <!-- 考察的是事件循环和回调队列。注意以下几点：
Promise 优先于 setTimeout 宏任务，所以 setTimeout 回调会最后执行
Promise 一旦被定义就会立即执行
Promise 的 resolve 和 reject  是异步执行的回调。所以 resolve() 会被放到回调队列中，在主函数执行完和 setTimeout 之前调用
await 执行完后，会让出线程。async 标记的函数会返回一个 Promise 对象 -->
</head>
<body>
  

<script type="text/javascript">
async function async1() {
    console.log('async1 start');
    await async2().then(() => {
        console.log("async2 end!")
    });
    console.log('async1 end');
}

async function async2(){
    console.log('async2');
}

console.log('script start');

setTimeout(function(){
    console.log('setTimeout');
},0)

async1();

new Promise(function(resolve) {
    console.log('promise1');
    resolve();
}).then(function(){
    console.log('promise2');
})

console.log('script end');
</script>
</body>
</html>